package dk.dashboardFP.panels.ui.text.label {
	import flash.events.Event;
	import flash.events.FocusEvent;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;

	/**
	 * @author jk
	 */
	public class InputLabel extends AbstractLabel {
		
		private var _sDefaultText : String="";
		private var _sInputText : String;
		
		private var _uintDefaultTextColor : uint;
		private var _uintActiveColor : uint;
		private var _bInDefault : Boolean = false;
		private var _bSelectAllOnFocusIn : Boolean=false;

		public function InputLabel(sText : String = "", sSize : Number = 12, uintColor : uint = 0x000000, bBold : Boolean = false, sFont : String = "") {
			
			super(sText, sSize, uintColor, bBold, sFont);
			
			_sDefaultText = sText;
			_sInputText="";
			
			setupInput();
			checkDefault();			
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// setup input
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function setupInput() : void {
			
			_tfLabel.autoSize = TextFieldAutoSize.NONE;
			nWidth=100;
			
			_tfLabel.type = TextFieldType.INPUT;
			_tfLabel.addEventListener(FocusEvent.FOCUS_IN, onFocusIn, false, 0, true);
			_tfLabel.addEventListener(FocusEvent.FOCUS_OUT, onFocusOut, false, 0, true);
			_tfLabel.selectable=true;
		}
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// intercept color sets
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		override public function set uintColor(oNewValue : uint) : void {
			_uintActiveColor = oNewValue;
			super.uintColor = oNewValue;
		}

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// input setup¨
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onFocusOut(event : FocusEvent) : void {
			if (_tfLabel.text== "") {
				if (_uintDefaultTextColor != _uintActiveColor) {
					super.uintColor = _uintDefaultTextColor;
				}
				sText=sDefaultText;
			}
		}

		private function onFocusIn(event : FocusEvent) : void {
			if (sText ==_sDefaultText) {
				if (super.uintColor != _uintActiveColor) {
					super.uintColor = _uintActiveColor;
				}
				sText="";
			}
			
			if (_bSelectAllOnFocusIn) {
				//_tfLabel.setSelection(0, _tfLabel.text.toString().length-1);
			}
			
			//_tfLabel.addEventListener(TextEvent.TEXT_INPUT, onInput, false, 0, true);
			_tfLabel.addEventListener(Event.CHANGE, onInput, false, 90000, true);
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// select on focus in
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function get bSelectAllOnFocusIn() : Boolean { 
			return _bSelectAllOnFocusIn; 
		}
		
		public function set bSelectAllOnFocusIn(oNewValue:Boolean) : void { 
			_bSelectAllOnFocusIn=oNewValue;
		}
		
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// text change listener
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		private function onInput(event : Event) : void {
			sText = _tfLabel.text;
			_sInputText=_tfLabel.text;
			checkDefault();
		}

		override public function set sText(sText : String) : void {
			if(sText == _sDefaultText) {
				setDefault();
			}
			super.sText = sText;
		}
		

		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// lock unlock
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		
		public function lock() : void {
			if (_tfLabel.type != TextFieldType.DYNAMIC) {
				_tfLabel.selectable=false;
				_tfLabel.type = TextFieldType.DYNAMIC;
			}
		}
		
		public function unLock() : void {
			if (_tfLabel.type!=TextFieldType.INPUT) {
				_tfLabel.selectable=true;
				_tfLabel.type = TextFieldType.INPUT;
			}
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// default handling
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		private function checkDefault() : void {
			if (_sInputText==sDefaultText && !_bInDefault) {
				setDefault();
			} else if (_sInputText!=sDefaultText && _bInDefault) {
				leaveDefault();
			}
		}
		
		private function leaveDefault() : void {
			_bInDefault=false;
			dispatchEvent(new InputLabelEvent(InputLabelEvent.LEFT_DEFAULT));
		}
		
		private function setDefault() : void {
			_bInDefault=true;
			dispatchEvent(new InputLabelEvent(InputLabelEvent.ENTERED_DEFAULT_VALUE));
		}
		
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// default setters
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

		public function get sDefaultText() : String {
			return _sDefaultText;
		}
		
		public function set sDefaultText(sDefaultText : String) : void {
			_sDefaultText = sDefaultText;
			if (_sInputText=="") {
				sText = _sDefaultText;
			}
		}
		
		public function get uintDefaultTextColor() : uint {
			return _uintDefaultTextColor;
		}
		
		public function set uintDefaultTextColor(uintDefaultTextColor : uint) : void {
			_uintDefaultTextColor = uintDefaultTextColor;
			super.uintColor = uintDefaultTextColor;
		}
		
		public function get bPassword() : Boolean { 
			return _tfLabel.displayAsPassword; 
		}
		
		public function set bPassword(oNewValue:Boolean) : void {
			_tfLabel.displayAsPassword = oNewValue; 
		}
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		// editablestatus
		//::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
		public function set editable(val:Boolean):void{
			if(val){
				_tfLabel.type = TextFieldType.INPUT;
			}else{
				_tfLabel.type = TextFieldType.DYNAMIC;
			}
		}
		public function get editable() : Boolean{
			if(_tfLabel.type == TextFieldType.INPUT){
				return(true);
			}else{
				return(false);
			}
		}
	}
}
